clear

infile imun_e isim_e tt_e fr_stolen_e pqcons_e mwage_e d_audit_e rcont_e funds_e zzpr_e pit_e psaving_e ieduc_e newit_e outelect_e newsavings_e newsteal_e newqcons_e value_elect_e nterms_e zzpu_e ipast_e mun_size_e wtp_e newsteal_nome_e audit3_e newage_e popsize_e psteal_e rnorm_e newpcons_e incumb_age_e util_e vemax_e d_run_e el_app_e using simdata_estimation_Apr2024.dat, clear
egen id = group(imun_e isim_e tt_e)
sort id
save "simdata_estimation.dta", replace

infile imun_b isim_b tt_b fr_stolen_b pqcons_b mwage_b d_audit_b rcont_b funds_b zzpr_b pit_b psaving_b ieduc_b newit_b outelect_b newsavings_b newsteal_b newqcons_b value_elect_b nterms_b zzpu_b ipast_b mun_size_b wtp_b newsteal_nome_b audit3_b newage_b popsize_b psteal_b rnorm_b newpcons_b incumb_age_b frac_fine_b potential_wage_b d_run_b el_app_b using "~/MyPapers/Mayors/Estimation/Stata/simdata_base_Apr2024.dat", clear
egen id = group(imun_b isim_b tt_b)
sort id
save "simdata_base.dta", replace

infile imun_1term isim_1term tt_1term fr_stolen_1term pqcons_1term mwage_1term d_audit_1term rcont_1term funds_1term zzpr_1term pit_1term psaving_1term ieduc_1term newit_1term outelect_1term newsavings_1term newsteal_1term newqcons_1term value_elect_1term nterms_1term zzpu_1term ipast_1term mun_size_1term wtp_1term newsteal_nome_1term audit3_1term newage_1term popsize_1term psteal_1term rnorm_1term newpcons_1term incumb_age_1term frac_fine_1term potential_wage_1term d_run_1term el_app_1term using "simdata_1term_Apr2024.dat", clear
egen id = group(imun_1term isim_1term tt_1term)
sort id
save "simdata_1term.dta", replace

infile imun_nss isim_nss tt_nss fr_stolen_nss pqcons_nss mwage_nss d_audit_nss rcont_nss funds_nss zzpr_nss pit_nss psaving_nss ieduc_nss newit_nss outelect_nss newsavings_nss newsteal_nss newqcons_nss value_elect_nss nterms_nss zzpu_nss ipast_nss mun_size_nss wtp_nss newsteal_nome_nss audit3_nss newage_nss popsize_nss psteal_nss rnorm_nss newpcons_nss incumb_age_nss frac_fine_nss potential_wage_nss d_run_nss el_app_nss using "simdata_no_sel_sav_Apr2024.dat", clear
egen id = group(imun_nss isim_nss tt_nss)
sort id
save "simdata_nss.dta", replace

infile imun_nobs_nun isim_nobs_nun tt_nobs_nun fr_stolen_nobs_nun pqcons_nobs_nun mwage_nobs_nun d_audit_nobs_nun rcont_nobs_nun funds_nobs_nun zzpr_nobs_nun pit_nobs_nun psaving_nobs_nun ieduc_nobs_nun newit_nobs_nun outelect_nobs_nun newsavings_nobs_nun newsteal_nobs_nun newqcons_nobs_nun value_elect_nobs_nun nterms_nobs_nun zzpu_nobs_nun ipast_nobs_nun mun_size_nobs_nun wtp_nobs_nun newsteal_nome_nobs_nun audit3_nobs_nun newage_nobs_nun popsize_nobs_nun psteal_nobs_nun rnorm_nobs_nun newpcons_nobs_nun incumb_age_nobs_nun frac_fine_nobs_nun potential_wage_nobs_nun d_run_nobs_nun el_app_nobs_nun using "simdata_no_sel_obs_unobs_Apr2024.dat", clear
egen id = group(imun_nobs_nun isim_nobs_nun tt_nobs_nun)
sort id
save "simdata_nobs_nun.dta", replace

infile imun_t isim_t tt_t fr_stolen_t pqcons_t mwage_t d_audit_t rcont_t funds_t zzpr_t pit_t psaving_t ieduc_t newit_t outelect_t newsavings_t newsteal_t newqcons_t value_elect_t nterms_t zzpu_t ipast_t mun_size_t wtp_t newsteal_nome_t audit3_t newage_t popsize_t psteal_t rnorm_t newpcons_t incumb_age_t frac_fine_t potential_wage_t d_run_t el_app_t using "simdata_terms_Apr2024.dat", clear
egen id = group(imun_t isim_t tt_t)
sort id
save "simdata_terms.dta", replace

infile imun_nr isim_nr tt_nr fr_stolen_nr pqcons_nr mwage_nr d_audit_nr rcont_nr funds_nr zzpr_nr pit_nr psaving_nr ieduc_nr newit_nr outelect_nr newsavings_nr newsteal_nr newqcons_nr value_elect_nr nterms_nr zzpu_nr ipast_nr mun_size_nr wtp_nr newsteal_nome_nr audit3_nr newage_nr popsize_nr psteal_nr rnorm_nr newpcons_nr incumb_age_nr frac_fine_nr potential_wage_nr d_run_nr el_app_nr using "simdata_norun_Apr2024.dat", clear
egen id = group(imun_nr isim_nr tt_nr)
sort id
save "simdata_norun.dta", replace

use "simdata_estimation.dta"
sort id

merge id using "simdata_base.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_1term.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_nss.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_nobs_nun.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_terms.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_norun.dta"
tab _merge
drop _merge
sort id

save simdata_all.dta, replace

* CHANGE THE PARAMETERS ETA AND THETA USING RECENTLY ESTIMATED ONES
gen eta = 0.93753645875196
gen deltaa = 2
gen theta = 0.06921015804913

gen isim_imun_b = imun_b*1000+isim_b
tsset isim_imun_b tt_b

quietly foreach y in _e _b _1term _nss _nobs_nun _t _nr {
  gen new_fr_stolen`y' = newsteal`y'/funds`y'
  gen newqcons_pc`y' = newqcons`y'/(popsize`y'^eta)
  gen l1_educ`y' = l.ieduc`y'
}

gen actual_incumb_age_b = (incumb_age_b+7)*4   
gen college_b = l1_educ_b==2
replace college_b = . if l1_educ_b==.

save simdata_all.dta, replace

use simdata_all.dta, replace

log using selection.log, replace

****************
*** TABLE 7A ***
****************

* The numbers are in the output file of the SMM estimation

****************
*** TABLE 7B ***
****************

gen d_fr_stolen_audit_e = (fr_stolen_e > 0 & d_audit_e == 1)
gen d_fr_stolen_e = (newsteal_e > 0)

*** MATCHING MOMENTS ***
* Fraction Incumbents Not Stealing (We need to add audit3_e == 1 if we want to include tt_e == 3)
tab d_fr_stolen_e if (d_audit_e == 1 | audit3_e == 1) & tt_e >= 2
* Mean of Log Ability, Second Term
* SEE OUTPUT FOR ESTIMATED MODEL 
* The number is in the output file of the SMM estimation
* 25th, Median, 75th, 90th percentiles Fraction of Funds Solen
pctile pct_new_fr_stolen_e = new_fr_stolen_e if tt_e >= 2, nq(100)
list pct_new_fr_stolen_e in 1/100
* Per-capita public consumption using the rivalry parameter
sum newqcons_pc_e if tt_e>=2

*************************************
*** MARGINAL RATE OF SUBSTITUTION ***
*************************************

sum popsize_e, d
scalar m_popsize = r(mean)
sum newpcons_e
scalar m_newpcons = r(mean)
gen Emrs = - m_popsize^eta/(theta*m_newpcons^delta)
sum Emrs

***********************************************
*** INTERPRETATION OF MEAS ERR IN FINE DATA ***
***********************************************

* Get the following numbers from 
scalar mu_fine = 0.0942693
scalar delta_fine = -0.78912147063386
scalar actual_fine = mu_fine+delta_fine
scalar frac_fine = exp(actual_fine)
di frac_fine

********************************************************************************************
*** UTILITY OF BEING IN POWER AS FRACTION OF UTILITY FROM PRIVATE AND PUBLIC CONSUMPTION ***
********************************************************************************************

scalar u_power = 1.29937335365349
sum vemax_e
scalar m_vemax_e = r(mean)
di u_power/m_vemax_e

****************
*** TABLES 6 ***
****************

* TABLE 8: Fraction Stolen, Public Consumption, Incumbent's Age, Initial Wealth, College Education, Ability, Electoral Appeal
* NOTE THAT WE REPORT PER-CAPITA PUBLIC CONSUMPTION ADJUSTED FOR THE DEGREE OF RIVALRY
gen ieduc_b_new = ieduc_b-1
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2 & nterms_b ==1
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2 & nterms_b ==2

****************
*** TABLES 7 ***
****************

* TABLE 9: Decomposing Selection from Incentives and Decisions

*** FRACTION STOLEN ***

* Difference in the data
sum new_fr_stolen_b if tt_b ==2 & nterms_b ==1
scalar fr_stolen_1st_term = r(mean)

sum new_fr_stolen_b if tt_b ==2 & nterms_b ==2
scalar fr_stolen_2nd_term = r(mean)

scalar perc_tot = (fr_stolen_1st_term-fr_stolen_2nd_term)/fr_stolen_1st_term
di perc_tot

* Difference generated by electoral incentives (all observable and unobservable are identical)
sum new_fr_stolen_1term if tt_1term ==2 & nterms_1term ==1 & nterms_b == 1
scalar var_1term = r(mean)
scalar perc_ei = (fr_stolen_1st_term-var_1term)/fr_stolen_1st_term
di perc_ei

* Differences generated by initial savings
sum new_fr_stolen_nss if tt_nss ==2 & nterms_nss ==1 & nterms_b == 1
scalar var_nss = r(mean)
scalar perc_nss = (fr_stolen_1st_term-var_nss)/fr_stolen_1st_term
di perc_nss

* Difference generated by observables and unobservabless
sum new_fr_stolen_nobs_nun if tt_nobs_nun ==2 & nterms_nobs_nun ==1 & nterms_b == 1
scalar var_nobs_nun = r(mean)
scalar perc_nobs_nun = (fr_stolen_1st_term-var_nobs_nun)/fr_stolen_1st_term
di perc_nobs_nun

*** PUBLIC CONSUMPTION ***

* Difference in the data
sum newqcons_pc_b if tt_b ==2 & nterms_b ==1
scalar var_1st_term = r(mean)

sum newqcons_pc_b if tt_b ==2 & nterms_b ==2
scalar var_2nd_term = r(mean)

scalar perc_tot = (var_1st_term-var_2nd_term)/var_1st_term
di perc_tot

* Difference generated by electoral incentives (all observable and unobservable are identical)
sum newqcons_pc_1term if tt_1term ==2 & nterms_1term ==1 & nterms_b == 1
scalar var_1term = r(mean)
scalar perc_ei = (var_1st_term-var_1term)/var_1st_term
di perc_ei

* Differences generated by initial savings
sum newqcons_pc_nss if tt_nss ==2 & nterms_nss ==1 & nterms_b == 1
scalar var_nss = r(mean)
scalar perc_nss = (var_1st_term-var_nss)/var_1st_term
di perc_nss

* Difference generated by obervables and unobservables
sum newqcons_pc_nobs_nun if tt_nobs_nun ==2 & nterms_nobs_nun ==1 & nterms_b == 1
scalar var_nobs_nun = r(mean)
scalar perc_nobs_nun = (var_1st_term-var_nobs_nun)/var_1st_term
di perc_nobs_nun

**********************************************************************************************
*** NUMBERS FOR ABILITY IN FIRST, SECOND AND THIRD TERM FOR SUBSECTION 3-TERM=LIMIT POLICY ***
**********************************************************************************************

sum newit_t if tt_t>=2
sum newit_t if tt_t>=2 & nterms_t == 1
scalar m_ability_1st = r(mean)
sum newit_t if tt_t>=2 & nterms_t == 2
scalar m_ability_2nd = r(mean)
sum newit_t if tt_t>=2 & nterms_t == 3
scalar m_ability_3rd = r(mean)

di (m_ability_3rd - m_ability_1st)/m_ability_1st
di (m_ability_3rd - m_ability_2nd)/m_ability_2nd

****************************************************************************
*** NUMBERS TO EXPLAIN STEALING DECLINE FOR NO-RUN POLICY IN SECOND TERM ***
****************************************************************************

sum newit_nr if tt_nr>=2
sum newit_nr if tt_nr>=2 & nterms_nr == 1
scalar m_ability_1st = r(mean)
sum newit_nr if tt_nr>=2 & nterms_nr == 2
scalar m_ability_2nd = r(mean)

di (m_ability_2nd - m_ability_1st)/m_ability_1st

sum newsavings_nr if tt_nr>=2
sum newsavings_nr if tt_nr>=2 & nterms_nr == 1
scalar m_sav_1st = r(mean)
sum newsavings_nr if tt_nr>=2 & nterms_nr == 2
scalar m_sav_2nd = r(mean)

di (m_sav_2nd - m_sav_1st)/m_sav_1st

log close
